package com.simple.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ReactorDemo {

    public static void main(String[] args) {
        Flux<String> flux = Flux.just("A", "B", "C");
        // 消费数据流
        flux.subscribe(System.out::println);

        List<String> list = Arrays.asList("A", "B", "C");
        list.stream().forEach(System.out::println);


        Flux<Integer> numbers = Flux.range(1, 10)
                // 过滤偶数
                .filter(n -> n % 2 == 0)
                // 平方
                .map(n -> n * n);
        // 输出结果
        numbers.subscribe(System.out::println);

        Mono<String> mono = Mono.fromSupplier(() -> {
            // 模拟耗时任务
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello, Reactor!";
        });
        // 异步订阅数据
        mono.subscribe(System.out::println);


        Flux.range(1, 5)
                .log()
                .map(i -> i * 2)
                .subscribe(System.out::println);

    }
}
